package me.passtime.projt.fee;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import me.passtime.projt.Home;
import me.passtime.projt.R;
import me.passtime.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-5
 * Time: 下午9:46
 */
public class PaymentPage extends Activity implements View.OnClickListener {
    private Button mUserInfo;
    private double amount = 0.0;
    private Handler mHandler;

    private EditText mPaymentAmount;
    private EditText mOtherPhoneNumber;
    private boolean otherPhone = false;

    private EditText mCardNumber;
    private EditText mCardPwd;
    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 18;
    private PaymentController mController;
    private IvrController mIvrController;

    String phoneNumber="";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment);
        mController=new PaymentController(this);
        mIvrController=new IvrController(this);
        mUserInfo = (Button) this.findViewById(R.id.payment_person_btn);
        mUserInfo.setText(UserHolder.phoneNumber + "\r\n" + UserHolder.userName);

        mPaymentAmount = (EditText) this.findViewById(R.id.payment_amount);
        mOtherPhoneNumber = (EditText) this.findViewById(R.id.other_number);

        mCardNumber = (EditText) this.findViewById(R.id.payment_card_number);
        mCardPwd = (EditText) this.findViewById(R.id.payment_card_pwd);

        this.findViewById(R.id.payment_this_phone).setOnClickListener(this);
        this.findViewById(R.id.payment_other_phone).setOnClickListener(this);
        this.findViewById(R.id.payment_next_btn).setOnClickListener(this);

        this.findViewById(R.id.payment_50_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_100_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_200_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_other_btn).setOnClickListener(this);

        this.findViewById(R.id.payment_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_exit).setOnClickListener(this);


        initHandler();
    }

    private void initHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    default:
                        break;
                }
            }
        };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.payment_this_phone:
                otherPhone = false;
                this.findViewById(R.id.payment_bg).setBackgroundDrawable(getResources().getDrawable(R.drawable.container_04));
                this.findViewById(R.id.other_number_line).setVisibility(View.GONE);
                break;
            case R.id.payment_other_phone:
                this.findViewById(R.id.payment_bg).setBackgroundDrawable(getResources().getDrawable(R.drawable.container_05));
                otherPhone = true;
                this.findViewById(R.id.other_number_line).setVisibility(View.VISIBLE);
                break;
            case R.id.payment_50_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        changeAmountBtnColors(R.id.payment_50_btn);
                        setPaymentAccount(50);
                    }
                });
                break;
            case R.id.payment_100_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        changeAmountBtnColors(R.id.payment_100_btn);
                        setPaymentAccount(100);
                    }
                });
                break;
            case R.id.payment_200_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        changeAmountBtnColors(R.id.payment_200_btn);
                        setPaymentAccount(200);
                    }
                });
                break;
            case R.id.payment_other_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        changeAmountBtnColors(R.id.payment_other_btn);
                        mPaymentAmount.setText("0");
                        mPaymentAmount.setVisibility(View.VISIBLE);
                    }
                });
                break;
            case R.id.payment_next_btn:
                doPaymentConfirm();
                break;
            case R.id.payment_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.payment_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.payment_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.payment_exit:
                AlertDialogUtils.showYesNoDiaLog(this,"是否退出系统?",new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
        }
    }

    private void changeAmountBtnColors(int id) {
        this.findViewById(R.id.payment_50_btn).setBackgroundDrawable(null);
        this.findViewById(R.id.payment_100_btn).setBackgroundDrawable(null);
        this.findViewById(R.id.payment_200_btn).setBackgroundDrawable(null);
        this.findViewById(R.id.payment_other_btn).setBackgroundDrawable(null);
        this.findViewById(id).setBackgroundDrawable(getResources().getDrawable(R.drawable.slider_03));
    }

    private void setPaymentAccount(int account) {
        mPaymentAmount.setText(account + "");
        mPaymentAmount.setVisibility(View.GONE);
    }

    private void doPaymentConfirm() {
        if (checkUserInput()) {
            try {
                amount = Double.valueOf(mPaymentAmount.getText().toString());
            } catch (NumberFormatException nfe) {
                AlertDialogUtils.showConfirmDiaLog(this, "金额输入错误");
                return;
            }
            if (otherPhone) {
                phoneNumber=mOtherPhoneNumber.getText().toString();
            } else {
                phoneNumber=UserHolder.phoneNumber;
            }
            String cardNo=mCardNumber.getText().toString().trim();
            String cardPwd=mCardPwd.getText().toString().trim();
            showDialog(DIALOG_PROGRESS);
            mIvrController.checkIsIVRCard(UserHolder.phoneNumber,cardNo,cardPwd,amount);
        }
    }

    public void doUnionPay(){
        mController.sendPaymentInfo(phoneNumber,amount);
    }

    private boolean checkUserInput() {

        String amount = mPaymentAmount.getText().toString();
        String otherPhoneNumber = mOtherPhoneNumber.getText().toString();
        String cardNumber = mCardNumber.getText().toString();
        String cardPwd = mCardPwd.getText().toString();
        if ("".equals(amount) || "0".equals(amount)) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入或选择金额");
            return false;
        } else if (otherPhone && "".equals(otherPhoneNumber)) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入其他号码");
            return false;
        }else if (otherPhone && !CheckUtils.isMobileNo(otherPhoneNumber)) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入正确的号码");
            return false;
        }else if ("".equals(cardNumber) || "".equals(cardPwd)) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入银行卡号信息");
            return false;
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
